package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.OreWarModule;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code= Protocol.S_ORE_WAR_REFUGE_DISPATCH, desc = "矿场避难所派遣")
public class OreRefugeDispatchCmd extends AbstractCommand {

    private int activityId;

    private ActivityInfo activityInfo;

    private int pitId;

    private List<Integer> patronsIdList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OreWarProto.OreRefugeDispatchReqMsg reqMsg = OreWarProto.OreRefugeDispatchReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        pitId = reqMsg.getPitId();
        patronsIdList = new ArrayList<>();
        for(int patronsId : reqMsg.getPatronsIdListList()){
            if(!patronsIdList.contains(patronsId)){
                patronsIdList.add(patronsId);
            }
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!OreWarMgr.isActivityActive(activityId)){
            ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_DAY_TIME;
            return;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        OreWarProto.OreRefugeDispatchRespMsg.Builder resMsg = OreWarProto.OreRefugeDispatchRespMsg.newBuilder();
        resMsg.setRet(ret);
        if(ret == 0 ){
            resMsg = player.getModule(OreWarModule.class).refugeDispatch(activityId, pitId, patronsIdList);
        }
        player.sendPacket(Protocol.U_ORE_WAR_REFUGE_DISPATCH,resMsg);

    }
}
